(function () {
   //收账开票登记页面数据显示，服务器分页
	var serverData ={};
	var	serverTable;
	jQuery(document).ready(function () {
		serverTable = jQuery('#dataTable').DataTable({
			"serverSide": true,
			"ordering": false,
			"ajax": {
				"dataType": "json",
				"type": "get",
				"cache": false,
				"url":window.rootPath+ '/ui/payment/getpage',
				"data": function (d) {
					return $.extend({}, d, serverData);
				}
			},
			columns: [
				{
					data: null
				},
				{
					data: null,
					defaultContent: "",
					render: function (data, type, full, meta) {
						var id=data.contractId
						var operate ='<input type="checkbox" class="checkchild">';
						return operate;
					}
				},
				{
					data: 'orderUnit',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
				},
				{
					data: 'projectName',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
				},
				{
					data: 'supplierCode',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
						}
					}
				},
				{
					data: 'totalSum'
				},
				{
					data: '0'
				}
			],
			"drawCallback": function (oSettings) {
				var k = 0;
				var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

				for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
					$('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
					k++;
				}
			}
		});
	});
	//修改合同计划
	jQuery('.editBtn').on('click', function () {
		var selects=getSelect("#dataTable",1);
		if(selects.length>1){
			$.springLemonDialog({
				content: '只能编辑一条记录',// 确认文本
				clickButton: function (sender, modal, index) {// 按钮事件处理
					$(this).closeDialog(modal);
				}
			});
		}
		else if(selects.length==0){
			$.springLemonDialog({
				content: '请选择要编辑的记录',// 确认文本
				clickButton: function (sender, modal, index) {// 按钮事件处理
					$(this).closeDialog(modal);
				}
			});
		}else if(selects.length==1){
			var id=selects[0].contractId;
			var contractType=selects[0].contractType;
			var url = window.rootPath + '/ui/payment/isInitial/'+id+'/'+contractType;
			jQuery.ajax({
				type: 'get',
				url: encodeURI(url),
				dataType: 'json',
				contentType: 'application/json;charset=utf-8',
				success: function (data) {
					if (data.code == 0) {
						$.springLemonDialog({
							content: '请先初始化收账计划',// 确认文本
							clickButton: function (sender, modal, index) {// 按钮事件处理
								$(this).closeDialog(modal);
							}
						});
						return;
					}
					else {
						window.location = window.rootPath + '/ui/payment/editSchedule/' + id + '/' + contractType;
					}
				}
			})
		}
	});
	//回款登记
	jQuery('.registPay').on('click', function () {
		var selects=getSelect("#dataTable",1);
		if(selects.length>1){
			$.springLemonDialog({
				content: '只能编辑一条记录',// 确认文本
				clickButton: function (sender, modal, index) {// 按钮事件处理
					$(this).closeDialog(modal);
				}
			});
		}
		else if(selects.length==0){
			$.springLemonDialog({
				content: '请选择要编辑的记录',// 确认文本
				clickButton: function (sender, modal, index) {// 按钮事件处理
					$(this).closeDialog(modal);
				}
			});
		}else if(selects.length==1){
			var id=selects[0].contractId;
			var contractType=selects[0].contractType;
			var url = window.rootPath + '/ui/payment/isInitial/'+id+'/'+contractType;
			jQuery.ajax({
				type: 'get',
				url: encodeURI(url),
				dataType: 'json',
				contentType: 'application/json;charset=utf-8',
				success: function (data) {
					if (data.code == 0) {
						$.springLemonDialog({
							content: '请先初始化收账计划',// 确认文本
							clickButton: function (sender, modal, index) {// 按钮事件处理
								$(this).closeDialog(modal);
							}
						});
						return;
					}
					else {
						window.location = window.rootPath + '/ui/payment/registPay/' + id + '/' + contractType;
					}
				}
			})
		}
	});
	//登记开票
	jQuery('.registPayment').on('click', function () {
		var selects=getSelect("#dataTable",1);
		if(selects.length>1){
			$.springLemonDialog({
				content: '只能编辑一条记录',// 确认文本
				clickButton: function (sender, modal, index) {// 按钮事件处理
					$(this).closeDialog(modal);
				}
			});
		}
		else if(selects.length==0){
			$.springLemonDialog({
				content: '请选择要编辑的记录',// 确认文本
				clickButton: function (sender, modal, index) {// 按钮事件处理
					$(this).closeDialog(modal);
				}
			});
		}else if(selects.length==1){
			var id=selects[0].contractId;
			var contractType=selects[0].contractType;
			var url = window.rootPath + '/ui/payment/isInitial/'+id+'/'+contractType;
			jQuery.ajax({
				type: 'get',
				url: encodeURI(url),
				dataType: 'json',
				contentType: 'application/json;charset=utf-8',
				success: function (data) {
					if (data.code == 0) {
						$.springLemonDialog({
							content: '请先初始化收账计划',// 确认文本
							clickButton: function (sender, modal, index) {// 按钮事件处理
								$(this).closeDialog(modal);
							}
						});
						return;
					}
					else {
						window.location = window.rootPath + '/ui/payment/registPayment/' + id + '/' + contractType;
					}
				}
			})
		}
	});
	//查询
	jQuery('.searchBtn').on('click',function(){
		serverData.key=jQuery('#search').val();
		serverTable.draw();
	})
	//撤销查询
	jQuery('.resetBtn').on("click",function()
	{
		jQuery('#search').val('');
		serverData.key ='';
		serverTable.draw();
	});
})();